module.exports = function ($scope, $resource, $stateParams, $modal, toaster, $timeout) {

    $scope.allSaleList = [];
    $scope.viewMap = {};
    //搜索信息
    $scope.searchParams = {
        channel_list_code: 'F10'
    };
    $scope.loadDate = function () {
        $resource('api/as/tc/sale/getSaleByChannelList', {}, {}).save($scope.searchParams, function (res) {
            if (res.errcode === 0) {
                $scope.viewMap = {};
                $scope.allSaleList = res.data;
                for (let index = 0; index < res.data.length; index++) {
                    const element = res.data[index];
                    if (!$scope.viewMap.hasOwnProperty(element.place_code)) {
                        $scope.viewMap[element.place_code] = {
                            'sale_name': element.place_name,
                            'img': element.img,
                            'place_name': element.place_name,
                            'company_name': element.company_name,
                            'address': element.address,
                            'saleList': []
                        }
                    }
                    $scope.viewMap[element.place_code].saleList.push(angular.copy(element));
                }
            } else {
                toaster.error({ title: element.name, body: res.errmsg });
            }
        });
    }
    $scope.loadDate();
	$scope.myKeyup = function (e) {
		//IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
		var keycode = window.event ? e.keyCode : e.which;
		if (keycode == 13) {
			$scope.loadDate();
		}
	};
    $scope.searchParams.provinceList = [];
    $scope.searchParams.cityList = [];
    $scope.searchArea = function (code) {
        $resource('/api/us/sc/city/arealist', {}, {}).save({ code: code }, function (res) {
            if (res.errcode === 0) {
                if (code) {
                    $scope.searchParams.cityList = res.data;
                    $scope.searchParams.cityList.unshift({
                        name: '--- 全部 ---',
                        code: ''
                    });
                    $scope.searchParams.city = $scope.searchParams.provinceList[0].code;
                } else {
                    $scope.searchParams.provinceList = res.data;
                    $scope.searchParams.provinceList.unshift({
                        name: '--- 全部 ---',
                        code: ''
                    });
                    $scope.searchParams.province = $scope.searchParams.provinceList[0].code;
                }
            } else {
                toaster.error({ title: element.name, body: res.errmsg });
            }
        });
    }
    $scope.searchArea();

    $scope.show_info = function (key, index) {
        $scope.viewMap[key].saleList[index].is_show_info = !$scope.viewMap[key].saleList[index].is_show_info;
    }

    $scope.order_info = {
        'name': '',
        'cardno': '',
        'mobile': '',
        'num': 0,
        'tour_date': '',
        'pay_method': 'account'
    };

    //单人下单
    $scope.buy = function (key, index) {
        let sale_info = $scope.viewMap[key].saleList[index];
        var modalInstance = $modal.open({
            template: require('../views/createOrderInfo.html'),
            controller: 'createOrderInfo',
            size: 'lg',
            resolve: {
                sale_info: function () {
                    return sale_info;
                }
            }
        });
        modalInstance.result.then(function (result) {
            $scope.order_info = result;
        });
    }

    //批量下单
    $scope.buyBatch = function (key, index) {
        let sale_info = $scope.viewMap[key].saleList[index];
        var modalInstance = $modal.open({
            template: require('../views/createOrderBatch.html'),
            controller: 'createOrderBatch',
            size: 'lg',
            resolve: {
                sale_info: function () {
                    return sale_info;
                }
            }
        });
        modalInstance.result.then(function (result) {
            $scope.order_info = result;
        });
    }

    //查看图片
    $scope.lookImg = function (url) {
        var modalInstance = $modal.open({
            template: require('../views/img.html'),
            controller: 'img',
            size: 'lg',
            resolve: {
                img: function () {
                    return url;
                }
            }
        });
        modalInstance.result.then(function (result) {
        });
    }

};